package June.studentsys;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student s1 = new Student(1, "小明", "12qwer");
        list.add(s1);
        loop:
        while (true) {
            System.out.println("-------学生管理系统---------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.打印学生");
            System.out.println("5.退出");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的选择");
            String sc = scanner.next();
            switch (sc) {
                case "1":
                    add(list);
                    break;
                case "2":
                    del(list);
                    break;
                case "3":
                    change(list);
                    break;
                case "4":
                    queryStu(list);
                    break;
                case "5":
                    System.out.println("已退出");
//                    System.exit(0);//停止虚拟机运行
                    break loop;
                default:
                    System.out.println("没有这个选项");
            }
        }
    }

    public static void add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请依次输入id,姓名，密码");
        int id = sc.nextInt();
        String name = sc.next();
        String password = sc.next();
        loop2:for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getId()) {
                System.out.println("添加失败，存在相同id的数据");
                break loop2;
            } else {
                Student student = new Student(id, name, password);
                list.add(student);
                System.out.println("添加成功");
            }
        }
    }

    public static void del(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生的id");
        int id = scanner.nextInt();
        for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getId()) {
                list.remove(i);
                System.out.println("删除成功");
            } else {
                System.out.println("不存在该id的学生,请核对后再操作");
                queryStu(list);
                return;
            }
        }
    }

    public static void change(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        queryStu(list);
        System.out.println("请选择要修改的学生id");
        int id = sc.nextInt();
        lopf:for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getId()) {
                System.out.println("请输入新的姓名和密码");
                String name = sc.next();
                String password = sc.next();
                list.get(i).setName(name);
                list.get(i).setPassword(password);
                System.out.println("修改成功");
            } else {
                System.out.println("未查找到有关id的学生");
                break lopf;
            }
        }

    }

    public static void queryStu(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无学生信息");
            return;
        }

        System.out.println("id\t姓名\t年龄");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.toString());
        }
    }
}
